package com.auemall.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.auemall.constant.ApplicationConstant;
import com.auemall.dao.BuyerGradesPOMapper;
import com.auemall.dao.TBuyerPOMapper;
import com.auemall.po.BuyerGradesPO;
import com.auemall.po.BuyerGradesPOExample;
import com.auemall.po.TBuyerPO;
import com.auemall.po.TBuyerPOExample;
import com.auemall.service.BuyerService;
import com.auemall.utils.EUDataGridResult;
import com.auemall.utils.RegexUtils;
import com.auemall.utils.StringUtils;
import com.auemall.vo.BaseRespVO;
import com.auemall.vo.BuyerSearchVO;
import com.auemall.vo.BuyerVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class BuyerServiceImpl implements BuyerService {
    private Logger logger = Logger.getLogger(BuyerServiceImpl.class);
    
	@Autowired
	private TBuyerPOMapper buyerMapper;
	
	@Autowired
	private BuyerGradesPOMapper buyerGradesMapper;
	
	
	public int deleteByPrimaryKey(String id) {
		String ids[] = id.split(",");
		int result = 0;
		if(ids !=null && ids.length > 0){
			for (String str : ids) {
				 result = buyerMapper.deleteByPrimaryKey(Integer.parseInt(str));
			}	
			
		}
		return result;
	}

	
	public int insert(TBuyerPO record) {
		
		return buyerMapper.insert(record);
	}

	
	public int insertSelective(TBuyerPO record) {
		return buyerMapper.insertSelective(record);
	}

	
	public BuyerVO selectByPrimaryKey(Integer id) {
		TBuyerPO buyerPO = buyerMapper.selectByPrimaryKey(id);
		
		//查询会员等级
		BuyerVO buyerVO = new BuyerVO();
    	BeanUtils.copyProperties(buyerPO, buyerVO);
    	BuyerGradesPO buyerGrade = buyerGradesMapper.selectByPrimaryKey(buyerPO.getLevel());
		buyerVO.setLevelName(buyerGrade.getName());
		
		return buyerVO;
	}


	@Override
	public BaseRespVO updateByPrimaryKeySelective(BuyerVO req) {
        BaseRespVO respVO = new BaseRespVO();
        if(null == req){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        if(req.getId()==null||req.getId()<0){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数[id]为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        
        //会员等级
        if(req.getLevel()==null||req.getLevel()<0){
        	respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请设置会员等级！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        
        BuyerGradesPO buyerGrades=buyerGradesMapper.selectByPrimaryKey(req.getLevel());
        if(buyerGrades==null){
        	respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("无效的会员等级！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        
        //查询出原始会员信息
        TBuyerPO record=buyerMapper.selectByPrimaryKey(req.getId());
        if(record==null){
        	respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("没有找到该会员！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        
        if(StringUtils.isBlank(req.getGender())){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("性别为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        
        if(StringUtils.isBlank(req.getMobile())){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("手机号为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        
        if(!RegexUtils.checkMobile(req.getMobile())){
        	 respVO.setResult(ApplicationConstant.RESULT_FALTURE);
             respVO.setMsg("请输入正确的手机号！");
             logger.error(respVO.getMsg());
             return respVO;
        }
        
        //手机号不能重复
        TBuyerPOExample example=new TBuyerPOExample();
        TBuyerPOExample.Criteria criteria = example.createCriteria();
        criteria.andMobileEqualTo(req.getMobile());
        criteria.andIdNotEqualTo(req.getId());
        
        List<TBuyerPO> list = buyerMapper.selectByExample(example);
        if(list!=null&&list.size()>0){
        	 respVO.setResult(ApplicationConstant.RESULT_FALTURE);
             respVO.setMsg("该手机号已注册！");
             logger.error(respVO.getMsg());
             return respVO;
        }
        
        record.setEmail(req.getEmail());
        record.setGender(req.getGender());
        record.setRealname(req.getRealname());
        record.setLevel(req.getLevel());
        record.setAddress(req.getAddress());
        record.setMobile(req.getMobile());
        record.setPhone(req.getPhone());
        record.setPostalcode(req.getPostalcode());
        record.setProvince(req.getProvince());
        record.setCity(req.getCity());
        record.setCounty(req.getCounty());
        record.setStreet(req.getStreet());
        record.setBirthday(req.getBirthday());
        record.setNickname(req.getNickname());
        
        buyerMapper.updateByPrimaryKeySelective(record);
        
        respVO.setResult(ApplicationConstant.RESULT_SUCCESS);
        respVO.setMsg("修改会员信息成功");
        return respVO;
	}


	@Override
	public int updateByPrimaryKey(BuyerVO record) {
		//return buyerMapper.updateByPrimaryKeySelective(record);
		return 0;
	}


	@Override
	public EUDataGridResult searchBuyer(BuyerSearchVO req,int page, int rows)
			throws Exception {
		EUDataGridResult respVO = new EUDataGridResult();
        if(null == req){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        //查询会员列表
        TBuyerPOExample example=new TBuyerPOExample();
        TBuyerPOExample.Criteria criteria = example.createCriteria();
        if(StringUtils.isNotBlank(req.getUserName())){
            criteria.andUsernameEqualTo(req.getUserName());
        }
        if(StringUtils.isNotBlank(req.getRealName())){
            criteria.andRealnameLike("%"+req.getRealName()+"%");
        }
        if(req.getUserLevel()!=null){
        	//查询会员等级
        	BuyerGradesPO buyerGrade = buyerGradesMapper.selectByPrimaryKey(req.getUserLevel());
        	if(buyerGrade != null){
        		criteria.andLevelEqualTo(buyerGrade.getId());        		
        	}
        }
        if(req.getSource()!=null){
            criteria.andSourceEqualTo(req.getSource());
        }
        PageHelper.startPage(page, rows);
        List<TBuyerPO> list = buyerMapper.selectByExample(example);
        
        //查询会员级别
        List<BuyerVO> buyers = new ArrayList<BuyerVO>();
        for (TBuyerPO buyer : list) {
        	BuyerVO buyerVO = new BuyerVO();
        	BeanUtils.copyProperties(buyer, buyerVO);
        	BuyerGradesPO buyerGrade = buyerGradesMapper.selectByPrimaryKey(buyer.getLevel());
			buyerVO.setLevelName(buyerGrade.getName());
        	buyers.add(buyerVO);
		}
        respVO.setRows(buyers);
        //取记录总条数
        PageInfo<TBuyerPO> pageInfo = new PageInfo<>(list);
        respVO.setTotal(pageInfo.getTotal());
        respVO.setResult(ApplicationConstant.RESULT_SUCCESS);
        respVO.setMsg("查询会员成功");
        return respVO;
	}
	


	@Override
	public int deleteBuyerGradesByPrimaryKey(String id) {
		String ids[] = id.split(",");
		int result = 0;
		if(ids !=null && ids.length > 0){
			for (String str : ids) {
				 result = buyerGradesMapper.deleteByPrimaryKey(Integer.parseInt(str));
			}	
			
		}
		
		return result;
	}

	@Override
	public int insertBuyerGrades(BuyerGradesPO record) {
		return buyerGradesMapper.insert(record);
	}


	@Override
	public BaseRespVO insertBuyerGradesSelective(BuyerGradesPO req) {
        BaseRespVO respVO = new BaseRespVO();
        if(null == req){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        
        if(StringUtils.isBlank(req.getName())){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("等级名称为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        
        if(req.getScore()==null||req.getScore()<=0){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("积分满足点数需大于0！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        
        if(req.getDiscount()==null||req.getDiscount()<=0||req.getDiscount()>100){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("折扣不 正确！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        
        if(req.getIsDefault()==null||req.getIsDefault()!=1){
        	req.setIsDefault(0);
        }else{
        	req.setIsDefault(1);
        }
        req.setId(null);
        
        buyerGradesMapper.insertSelective(req);
        
        respVO.setResult(ApplicationConstant.RESULT_SUCCESS);
        respVO.setMsg("新增会员等级成功");
        return respVO;
	}


	@Override
	public BuyerGradesPO selectBuyerGradesByPrimaryKey(Integer id) {
		return buyerGradesMapper.selectByPrimaryKey(id);
	}


	@Override
	public BaseRespVO updateBuyerGradesByPrimaryKeySelective(BuyerGradesPO req) {
        BaseRespVO respVO = new BaseRespVO();
        if(null == req){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        
        if(req.getId()==null||req.getId()<=0){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("等级ID不正确！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        
        if(StringUtils.isBlank(req.getName())){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("等级名称为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        
        if(req.getScore()==null||req.getScore()<=0){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("积分满足点数需大于0！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        
        if(req.getDiscount()==null||req.getDiscount()<=0||req.getDiscount()>100){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("折扣不 正确！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        
        if(req.getIsDefault()==null||req.getIsDefault()!=1){
        	req.setIsDefault(0);
        }else{
        	req.setIsDefault(1);
        }
        
        BuyerGradesPO buyerGrades= buyerGradesMapper.selectByPrimaryKey(req.getId());
        
        if(buyerGrades==null){
        	respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("会员等级不存在！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        
        buyerGradesMapper.updateByPrimaryKeySelective(req);
        
        respVO.setResult(ApplicationConstant.RESULT_SUCCESS);
        respVO.setMsg("编辑会员等级成功");
        return respVO;
	}


	//buyerGradesMapper
	@Override
	public EUDataGridResult searchBuyerGrade(BuyerGradesPO req, int page, int rows) throws Exception {
		EUDataGridResult respVO = new EUDataGridResult();
        if(null == req){
            respVO.setResult(ApplicationConstant.RESULT_FALTURE);
            respVO.setMsg("请求参数为空！");
            logger.error(respVO.getMsg());
            return respVO;
        }
        
        
        //查询会员等级列表
        BuyerGradesPOExample example=new BuyerGradesPOExample();
        //BuyerGradesPOExample.Criteria criteria = example.createCriteria();
        
        PageHelper.startPage(page, rows);
        List<BuyerGradesPO> list = buyerGradesMapper.selectByExample(example);
        respVO.setRows(list);
        //取记录总条数
        PageInfo<BuyerGradesPO> pageInfo = new PageInfo<>(list);
        respVO.setTotal(pageInfo.getTotal());
        respVO.setResult(ApplicationConstant.RESULT_SUCCESS);
        respVO.setMsg("查询会员成功");
        return respVO;
	}

}
